package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TemporaryPowerWorkBo;
import org.dromara.system.domain.vo.TemporaryPowerWorkVo;
import org.dromara.system.domain.TemporaryPowerWork;
import org.dromara.system.mapper.TemporaryPowerWorkMapper;
import org.dromara.system.service.ITemporaryPowerWorkService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 临时用电作业Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TemporaryPowerWorkServiceImpl implements ITemporaryPowerWorkService {

    private final TemporaryPowerWorkMapper baseMapper;

    /**
     * 查询临时用电作业
     *
     * @param id 主键
     * @return 临时用电作业
     */
    @Override
    public TemporaryPowerWorkVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询临时用电作业列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 临时用电作业分页列表
     */
    @Override
    public TableDataInfo<TemporaryPowerWorkVo> queryPageList(TemporaryPowerWorkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TemporaryPowerWork> lqw = buildQueryWrapper(bo);
        Page<TemporaryPowerWorkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的临时用电作业列表
     *
     * @param bo 查询条件
     * @return 临时用电作业列表
     */
    @Override
    public List<TemporaryPowerWorkVo> queryList(TemporaryPowerWorkBo bo) {
        LambdaQueryWrapper<TemporaryPowerWork> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TemporaryPowerWork> buildQueryWrapper(TemporaryPowerWorkBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TemporaryPowerWork> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TemporaryPowerWork::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getZyzbh()), TemporaryPowerWork::getZyzbh, bo.getZyzbh());
        lqw.eq(StringUtils.isNotBlank(bo.getSqdw()), TemporaryPowerWork::getSqdw, bo.getSqdw());
        lqw.eq(bo.getZysqsj() != null, TemporaryPowerWork::getZysqsj, bo.getZysqsj());
        lqw.eq(StringUtils.isNotBlank(bo.getSqr()), TemporaryPowerWork::getSqr, bo.getSqr());
        lqw.eq(StringUtils.isNotBlank(bo.getZynr()), TemporaryPowerWork::getZynr, bo.getZynr());
        lqw.eq(StringUtils.isNotBlank(bo.getZywz()), TemporaryPowerWork::getZywz, bo.getZywz());
        lqw.eq(StringUtils.isNotBlank(bo.getJrdydgl()), TemporaryPowerWork::getJrdydgl, bo.getJrdydgl());
        lqw.eq(StringUtils.isNotBlank(bo.getSbmedgl()), TemporaryPowerWork::getSbmedgl, bo.getSbmedgl());
        lqw.eq(StringUtils.isNotBlank(bo.getGzdy()), TemporaryPowerWork::getGzdy, bo.getGzdy());
        lqw.eq(StringUtils.isNotBlank(bo.getYdr()), TemporaryPowerWork::getYdr, bo.getYdr());
        lqw.eq(StringUtils.isNotBlank(bo.getZyryxx()), TemporaryPowerWork::getZyryxx, bo.getZyryxx());
        lqw.eq(StringUtils.isNotBlank(bo.getZydwlx()), TemporaryPowerWork::getZydwlx, bo.getZydwlx());
        lqw.eq(StringUtils.isNotBlank(bo.getZydwmc()), TemporaryPowerWork::getZydwmc, bo.getZydwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getZyfzr()), TemporaryPowerWork::getZyfzr, bo.getZyfzr());
        lqw.eq(bo.getZysskssj() != null, TemporaryPowerWork::getZysskssj, bo.getZysskssj());
        lqw.eq(bo.getZyssjssj() != null, TemporaryPowerWork::getZyssjssj, bo.getZyssjssj());
        lqw.eq(StringUtils.isNotBlank(bo.getSfsjqttszy()), TemporaryPowerWork::getSfsjqttszy, bo.getSfsjqttszy());
        lqw.eq(StringUtils.isNotBlank(bo.getSjqttszyzbh()), TemporaryPowerWork::getSjqttszyzbh, bo.getSjqttszyzbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFxbs()), TemporaryPowerWork::getFxbs, bo.getFxbs());
        lqw.eq(StringUtils.isNotBlank(bo.getAqjdr()), TemporaryPowerWork::getAqjdr, bo.getAqjdr());
        lqw.eq(StringUtils.isNotBlank(bo.getJsjdr()), TemporaryPowerWork::getJsjdr, bo.getJsjdr());
        lqw.eq(StringUtils.isNotBlank(bo.getJhr()), TemporaryPowerWork::getJhr, bo.getJhr());
        lqw.eq(StringUtils.isNotBlank(bo.getDqzt()), TemporaryPowerWork::getDqzt, bo.getDqzt());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), TemporaryPowerWork::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), TemporaryPowerWork::getLatitude, bo.getLatitude());
        lqw.eq(bo.getStartTime() != null, TemporaryPowerWork::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, TemporaryPowerWork::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSpgbbm()), TemporaryPowerWork::getSpgbbm, bo.getSpgbbm());
        lqw.eq(StringUtils.isNotBlank(bo.getFhyp()), TemporaryPowerWork::getFhyp, bo.getFhyp());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachment()), TemporaryPowerWork::getAttachment, bo.getAttachment());
        return lqw;
    }

    /**
     * 新增临时用电作业
     *
     * @param bo 临时用电作业
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TemporaryPowerWorkBo bo) {
        TemporaryPowerWork add = MapstructUtils.convert(bo, TemporaryPowerWork.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改临时用电作业
     *
     * @param bo 临时用电作业
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TemporaryPowerWorkBo bo) {
        TemporaryPowerWork update = MapstructUtils.convert(bo, TemporaryPowerWork.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TemporaryPowerWork entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除临时用电作业信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
